#include "stdafx.h"
#include "wf_drawlinerectangle.h"
#include "pf_snapper.h"



WF_DrawLineRectangle::WF_DrawLineRectangle(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw rectangles",
                           container, graphicView) {
    reset();
}



WF_DrawLineRectangle::~WF_DrawLineRectangle() {}

QAction* WF_DrawLineRectangle::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	//  tr("&Rectangle"),
	QAction* action = new QAction(tr("Rectangle"), NULL);
	action->setIcon(QIcon(":/extui/linesrect.png"));
    //action->zetStatusTip(tr("Draw rectangles"));
	return action;
}


void WF_DrawLineRectangle::reset() {
    for (int i=0; i<4; ++i) {
        data[i] = PF_LineAttribute(PF_Vector(false),
                              PF_Vector(false));
    }
}



void WF_DrawLineRectangle::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();
}



void WF_DrawLineRectangle::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_Line* line[4];
    preparePreview();

    // create and add rectangle:
    for (int i=0; i<4; ++i) {
        line[i] = new PF_Line(container,
                              data[i]);
        line[i]->setLayerToActive();
        line[i]->setPenToActive();
        container->addEntity(line[i]);
    }

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        for (int i=0; i<4; ++i) {
            document->addUndoable(line[i]);
        }
        document->endUndoCycle();
    }

    // upd. view
	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(corner2);
}



void WF_DrawLineRectangle::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLineRectangle::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);
    if (getStatus()==SetCorner2 && corner1.valid) {
        corner2 = mouse;
        deletePreview();

        preparePreview();

        for (int i=0; i<4; ++i) {
            preview->addEntity(new PF_Line(preview, data[i]));
        }
        drawPreview();
    }

    PF_DEBUG->print("PF_ActionDrawLineRectangle::mouseMoveEvent end");
}


void WF_DrawLineRectangle::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawLineRectangle::preparePreview() {
    data[0] = PF_LineAttribute(corner1, PF_Vector(corner2.x, corner1.y));
    data[1] = PF_LineAttribute(PF_Vector(corner2.x, corner1.y), corner2);
    data[2] = PF_LineAttribute(corner2, PF_Vector(corner1.x, corner2.y));
    data[3] = PF_LineAttribute(PF_Vector(corner1.x, corner2.y), corner1);
}


void WF_DrawLineRectangle::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetCorner1:
        corner1 = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetCorner2);
        break;

    case SetCorner2:
        corner2 = mouse;
        trigger();
        setStatus(SetCorner1);
        break;

    default:
        break;
    }
}



void WF_DrawLineRectangle::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }
}



PF_StringList WF_DrawLineRectangle::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}


void WF_DrawLineRectangle::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetCorner1:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
                                                tr("Cancel"));
            break;
        case SetCorner2:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
                                                tr("Back"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}


void WF_DrawLineRectangle::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}


void WF_DrawLineRectangle::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
        }
    }
}

// EOF
